#include "hplcdebuginfopage.h"
#include <QDebug>
#include "modulemanager.h"
#include "modulehplcpump.h"
#include "modulehplcpumpparamiddefine.h"
#include "modulehplccmdid.h"
#include <QCloseEvent>
#include "modulehplccmdid.h"

HplcDebugInfoPage::HplcDebugInfoPage(QWidget *parent)
    : QWidget(parent)
{
    setWindowTitle(tr("Hplc Debug Info Page"));
    setAttribute(Qt::WA_DeleteOnClose);

    ModuleHplcPump *mod = static_cast<ModuleHplcPump*>(moduleManager->find(ModuleID_HPLC_PumpA));
    if (mod == nullptr)
        return;
    connect(mod, &ModuleHplcPump::parseCompleted,
            this, &HplcDebugInfoPage::onParseCompleted);

    m_teDebugInfo = new QTextEdit();

    m_pbClear = new QPushButton(tr("Clear"));
    connect(m_pbClear, &QPushButton::clicked, this, [=](){
        m_teDebugInfo->clear();
    });
    m_pbPrintVaiants = new QPushButton(tr("Print Variants"));
    connect(m_pbPrintVaiants, &QPushButton::clicked, this, [=](){
        QVariant var;
        mod->setProtocolCmd(hplc::CommonSystemCmdID_SetDebugVariants, var);
    });

    m_lbDebugPort = new QLabel(tr("Debug Port"));
    m_leDebugPort = new QLineEdit();
    m_pbSetDebugPort = new QPushButton(tr("Set"));
    connect(m_pbSetDebugPort, &QPushButton::clicked, this, [=](){
        QVariant var;
        quint8 port = m_leDebugPort->text().toUInt();
        var.setValue(port);
        mod->setProtocolCmd(hplc::CommonSystemCmdID_SetDebugPort, var);
    });
    m_pbGetDebugPort = new QPushButton(tr("Get"));
    connect(m_pbGetDebugPort, &QPushButton::clicked, this, [=](){
        QVariant var;
        mod->getProtocolCmd(hplc::CommonSystemCmdID_GetDebugPort, var);
    });

    QHBoxLayout *hblDebugPort = new QHBoxLayout();
    hblDebugPort->addWidget(m_lbDebugPort);
    hblDebugPort->addWidget(m_leDebugPort);
    hblDebugPort->addWidget(m_pbSetDebugPort);
    hblDebugPort->addWidget(m_pbGetDebugPort);

    QHBoxLayout *hblClear = new QHBoxLayout();
    hblClear->addWidget(m_pbClear);
    hblClear->addWidget(m_pbPrintVaiants);
    hblClear->addLayout(hblDebugPort);

    QVBoxLayout *vbl = new QVBoxLayout();
    vbl->addWidget(m_teDebugInfo);
    vbl->addLayout(hblClear);
    setLayout(vbl);
}

HplcDebugInfoPage::~HplcDebugInfoPage()
{

}

void HplcDebugInfoPage::closeEvent(QCloseEvent *event)
{
    disconnect();
    event->accept();
}

void HplcDebugInfoPage::onParseCompleted(int id, const QVariant &var)
{
    switch (id)
    {
        case hplc::CommonSystemCmdID_SetDebug:
        {
            m_teDebugInfo->append(var.toString());
        }
        break;
        case hplc::CommonSystemCmdID_SetDebugPort:
        {
            quint8 port = var.toUInt();
            m_leDebugPort->setText(QString::number(port));
        }
        break;
        default:
        break;
    }
}
